﻿function HtmlEncode(text){
    var re = {
        '<':'&lt;',
        '>':'&gt;',
        '&':'&amp;',
        '"':'&quot;'
    };
    for (i in re) text = text.replace(new RegExp(i,'g'), re[i]);
    return text;
}

function HtmlDecode(text){
    var re = {
        '&lt;':'<',
        '&gt;':'>',
        '&amp;':'&',
        '&quot;':'"'
    };
    for (i in re) text = text.replace(new RegExp(i,'g'), re[i]);
    return text;
}

function UTF8UrlEncode(input){
    var output = "";
    var currentChar = '';

    for(var counter = 0; counter < input.length; counter++) {
        currentChar = input.charCodeAt(counter);
        if((48 <= currentChar) && (currentChar <= 57))
            output = output + input.charAt(counter);
        else if((65 <= currentChar) && (currentChar <= 90))
            output = output + input.charAt(counter);
        else if((97 <= currentChar) && (currentChar <= 122))
            output = output + input.charAt(counter);
        else
            output = output + UTF8UrlEncodeChar(currentChar);
    }
    return output;
}
function UTF8UrlEncodeChar(input){
    if(input <= 0x7F) return "%" + input.toString(16);

    var leadByte = 0xFF80; var hexString = ""; var leadByteSpace = 5;
    while(input > (Math.pow(2, leadByteSpace + 1) - 1)){
        hexString = "%" + ((input & 0x3F) | 0x80).toString(16) + hexString;
        leadByte = (leadByte >> 1);
        leadByteSpace--;
        input = input >> 6;
    }
    return ("%" + (input | (leadByte & 0xFF)).toString(16) + hexString).toUpperCase();
}
//加载Script
function LoadScript(tag,file){
    var scripttag = $("loadscript"+tag);
    var head = document.getElementsByTagName("head").item(0);
    if(scripttag){
        head.removeChild(scripttag);
    }
    script = document.createElement("script");
    with(script){
        src  = ""+file;
        type = "text/javascript";
        id   = "loadscript"+tag;
        }
    head.appendChild(script);
}
//加载Css
function LoadCss(tag,file){
    var csstag = $("loadcss"+tag);
    var head = document.getElementsByTagName("head").item(0);
    if(csstag){
        head.removeChild(csstag);
    }
    css = document.createElement("link");
    with(css){
        href = ""+file;
        rel  = "stylesheet";
        type = "text/css";
        id   = "loadcss"+tag;
        }
    head.appendChild(css);
}
String.prototype.Trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function() {
    return this.replace(/(^\s*)/g, "");
}
String.prototype.RTrim = function() {
    return this.replace(/(\s*$)/g, "");
}
//日期转换
Date.prototype.Format = function(fmt){
    //author: meizz
    //例子1 (new Date()).Format("yyyy年MM月dd日 hh时mm分ss秒");
    //例子2 (new Date()).Format("yyyy年M月d日 h时m分s秒");
    //例子3 (new Date()).Format("yyyy年MM月dd日 hh时mm分ss秒S毫秒");
    var o = {
        "M+" : this.getMonth()+1, //月份
        "d+" : this.getDate(), //日
        "h+" : this.getHours(), //小时
        "m+" : this.getMinutes(), //分
        "s+" : this.getSeconds(), //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S" : this.getMilliseconds() //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}
//判断字符串是否符合日期填写规则
//("2007-1-1").isDate()
String.prototype.isDate = function(){
    var r = this.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    if(r==null){
        return false;
    }
    var d = new Date(r[1], r[3]-1, r[4]);
    return(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}
//判断字符串是否符合日期+时间填写规则
//("2007-1-1 1:1:1").isTime()
String.prototype.isTime = function(){
    var r = this.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
    if(r==null){
        return false;
    }
    var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
    return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);
}
//时间刷新
function getTime(){
    $("lblTime").innerHTML=(new Date()).Format("yyyy年MM月dd日 hh时mm分ss秒");
    setTimeout("getTime()",1000);
}

//检测字符串长度
function getValueLength(str,b){
    if(b){//汉字按2字节计算
        return str.replace(/[^\x00-\xff]/gi,'xx').length;
    }else{//汉字按1字节计算
        return str.length;
    }
}



function iniControlsEvent(frms){
    var MSIE=Prototype.Browser.IE;
    for(i=0;i<frms.length;i++){
        e=frms.elements[i];
        with(e){
            switch(className.toLowerCase()){
                case "button"://普通按纽
                    if(MSIE){
                        attachEvent("onmouseover",function(){
                            className="topanniu2";
                        });
                        attachEvent("onmouseout",function(){
                            className="topanniu";
                        });
                    }else{
                        addEventListener("mouseover",function(){
                            className="topanniu2";
                        },false);
                        addEventListener("mouseout",function(){
                            className="topanniu";
                        },false);
                    }
                    break;
                case "text"://文本框
                    if(MSIE){
                        attachEvent("onfocus",function(){
                            style.backgroundColor="#FFFADF";select();
                        });
                        attachEvent("onblur",function(){
                            style.backgroundColor="#FFFFFF";
                        });
                    }else{
                        addEventListener("focus",function(){
                            style.backgroundColor="#FFFADF";select();
                        },false);
                        addEventListener("blur",function(){
                            style.backgroundColor="#FFFFFF";
                        },false);
                    }
                    break;
            }
            }
    }
}

function changeBigButtonCss(obj,b){
    var MSIE=Prototype.Browser.IE;
    var elm=$(obj);
    with(elm){
        className=(b?"topanniu":"topanniu12");
        disabled=(b?false:true);
        if(MSIE){
            onmouseout=function(){
                className="topanniu";
            };
            onmouseover=function(){
                className=(b?"topanniu2":"topanniu12");
            };
        }else{

            addEventListener("mouseout",function(){
                className="topanniu";
            },false);
            addEventListener("mouseover",function(){
                className=(b?"topanniu2":"topanniu12");
            },false);
        }
        }
}

function changeSmlButtonCss(obj,b){
    var MSIE=Prototype.Browser.IE;
    var elm=$(obj);
    with(elm){
        className=(b?"bt1":"bt1h");
        disabled=(b?false:true);
        if(MSIE){
            onmouseout=function(){
                className="bt1";
            };
            onmouseover=function(){
                className=(b?"bt2":"bt1h");
            };
        }else{
            addEventListener("mouseout",function(){
                className="bt1";
            },false);
            addEventListener("mouseover",function(){
                className=(b?"bt2":"bt1h");
            },false);
        }
        }
}

function changeSmlButtonCss2(obj,b){
    var MSIE=Prototype.Browser.IE;
    var elm=$(obj);
    with(elm){
        className=(b?"topanniu":"topanniuh");
        disabled=(b?false:true);
        if(MSIE){
            onmouseout=function(){
                className="topanniu2";
            };
            onmouseover=function(){
                className=(b?"topanniu":"topanniuh");
            };
        }else{
            addEventListener("mouseout",function(){
                className="topanniu2";
            },false);
            addEventListener("mouseover",function(){
                className=(b?"topanniu":"topanniuh");
            },false);
        }
        }
}

function getChecksValue(tg,obj){
    var result01="",result02=0,result03="";
    var chks=document.getElementsByName(tg);
    for(var i=0;i<chks.length;i++){
        e=chks[i];
        if(obj){
            e.checked=obj.checked;
        }
        if(e.checked){
            if(result01.length==0){
                result01=e.value;
                result03=e.title;
            }else{
                result01+=","+e.value;
                result03+=","+e.title;
            }
            result02+=1;
        }
    }
    return {
        id:result01,
        cnt:result02,
        name:result03
    };
}

//浮动显示二级子目录js
function GetScrollXY(){
    var x=0,y=0
    if(document.documentElement.scrollTop){
        x=document.documentElement.scrollLeft;
        y=document.documentElement.scrollTop;
    }
    else{
        x=document.body.scrollLeft;
        y=document.body.scrollTop;
    }
    return {
        x:x,
        y:y
    }
}

window._stopMouseOver=function(e){
    (window.event||e).cancelBubble=true;
}

function showUserCard(e, url)
{
    e = (window.event||e); e.cancelBubble=true;
    var img = e.srcElement || e.target;
    if(!url) return;
    var layer=document.getElementById("UserCard_layer");
    if(!layer)
        return;
    layer.style.display="";
    var xy;
    if(navigator.userAgent.toLowerCase().indexOf("opera")<0)
    {
        xy=realOffset(img); layer.style.left = (xy.x+img.offsetWidth-2) +"px";
    }
    else//is opera
    {
        xy=getEventXY(e); layer.style.left = xy.x +"px";
    }
    layer.style.top  = (xy.y) +"px";

    new Ajax.Request(url, {
        method: 'get',
        asynchronous:true,
        evalScripts:true
    });

}

document.attachEvent("onmouseover", function(){
    try{
        document.getElementById("UserCard_layer").style.display="none";
    }catch(ex){}
    });

function getEventXY(e){
    var posx=0,posy=0;
    if(e==null) e=window.event;
    if(e.pageX || e.pageY)
    {
        posx=e.pageX; posy=e.pageY;
    }
    else if(e.clientX || e.clientY)
    {
        posx=e.clientX+GetScrollXY().x;
        posy=e.clientY+GetScrollXY().y;
    }
    return {
        "x":posx,
        "y":posy
    };
}

function realOffset(o)
{
    if(!o) return null; var e=o, x=y=l=t=0;
    do{
        l+=e.offsetLeft||0; t+=e.offsetTop||0; e=e.offsetParent;
    }while(e);
    do{
        x+=o.scrollLeft||0; y+=o.scrollTop||0; o=o.parentNode;
    }while(o);
    var xy=GetScrollXY();
    return {
        "x":l-x+xy.x,
        "y":t-y+xy.y
        };
};
